import Vue from "vue";
import {request} from "../../../util/request"
import "../../../common/common.less";
import "./list.less";
import dialog from "../../../util/dialog.js";
import pageing from "../../../components/pageing/pageing";
new Vue({
    el:"#louie",
    data:{
        handle_index:0,  //操作的下标
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        list:[]   //列表
    },
    created:function(){
        this.getTotal()
            .then(()=>{
                this.getList();
            });
    },
    components:{
        pageing:pageing
    },
    methods:{
        /**
         * 获取列表数据总数
         */
        getTotal(){
            return new Promise((success)=>{
                request('goods/getTotal')
                    .then((res)=>{
                        if(res.data.status === "success"){
                            this.total = parseInt(res.data.data);
                            success();
                        }
                    });
            });
        },
        /**
         * 获取列表
         */
        getList(){
            request("goods/getList",{
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.list = res.data.data;
                    }
                });
        },
        /**
         * 切换页面
         */
        changePage(page){
            this.pageNum = page;
            this.getList();
        },
        /**
         * 前往编辑商品
         */
        toEdit(res){
            let editBtn = res.target;
            window.location.href = editBtn.getAttribute("data-url")+'&goods_id='+editBtn.getAttribute("data-goods-id");
        },
        /**
         * 点击切换商品上架状态
         */
        clickStatus(index){
            this.handle_index = index;
            if(this.list[index].status === '1'){
                this.changeStatus(2);
            }else{
                this.changeStatus(1);
            }
        },
        /**
         *删除
         */
        del(index){
            this.handle_index = index;
            dialog.confirm(this,"确定要删除该商品吗？",(res)=>{
                if(res){
                    this.changeStatus(9);
                }
            });
        },
        /**
         * 修改商品状态
         */
        changeStatus(status){
            request("goods/changeStatus",{
                goods_id:this.list[this.handle_index].id,
                status
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getList();
                    }else{
                        dialog.alert(this,"操作失败");
                    }
                });
        }
    }
});